package com.library.control.log;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractFormController;

import com.library.model.dto.booksearch.BookSearchDTO;
import com.library.model.service.BookSearchService;

public class BookSearchController extends AbstractFormController {

	private BookSearchService booksearchservice;
	
	public void setBooksearchservice(BookSearchService booksearchservice) {
		this.booksearchservice = booksearchservice;
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String book=request.getParameter("searchname");
		String search=request.getParameter("search");
		BookSearchDTO dto=new BookSearchDTO();
	
		if("book_publisher".equals(search)){
			dto.setBook_publisher(book);
		}else if("book_name".equals(search)){
			dto.setBook_name(book);
		}else{
			dto.setBook_author(book);
		}
		return dto;
	}	
		
		
	@Override
	protected ModelAndView processFormSubmission(HttpServletRequest request,
			HttpServletResponse arg1, Object arg2, BindException arg3)
			throws Exception {
		System.out.println(arg2);
		ArrayList<BookSearchDTO> list=(ArrayList<BookSearchDTO>)booksearchservice.searchBooks((BookSearchDTO)arg2);
		System.out.println(list.get(0).getBook_author());
		ModelAndView mav=new ModelAndView();
		mav.addObject("books", list);
		mav.setViewName("booksearch");
		return mav;
	}

	@Override
	protected ModelAndView showForm(HttpServletRequest arg0,
			HttpServletResponse arg1, BindException arg2) throws Exception {
		// TODO Auto-generated method stub
		return new ModelAndView("booksearch");
	}

}
